import { Controller, Get, Inject, Provide, Query } from '@midwayjs/decorator';

import { BaseController, ERRCODE } from '../../../comm/controller';
import { CommAddressService } from '../service/address';

@Provide()
@Controller('/comm/address')
export class CommAddressController extends BaseController {
  @Inject()
  commAddressService: CommAddressService;

  // 以下是获取省份、城市、区域、街道信息的路由
  @Get('/province')
  async getProvince() {
    try {
      const data = await this.commAddressService.getProvince();
      return this.ok({ provinces: data });
    } catch (err) {
      return this.fail(ERRCODE.SERVER_COMM_ERROR, err.message);
    }
  }

  @Get('/city')
  async getCity(@Query() provinceCode: string) {
    try {
      const data = await this.commAddressService.getCity(provinceCode);
      return this.ok({ cities: data });
    } catch (err) {
      return this.fail(ERRCODE.SERVER_COMM_ERROR, err.message);
    }
  }

  @Get('/area')
  async getArea(@Query() cityCode: string) {
    try {
      const data = await this.commAddressService.getArea(cityCode);
      return this.ok({ areas: data });
    } catch (err) {
      return this.fail(ERRCODE.SERVER_COMM_ERROR, err.message);
    }
  }

  @Get('/street')
  async getStreet(@Query() areaCode: string) {
    try {
      const data = await this.commAddressService.getStreet(areaCode);
      return this.ok({ streets: data });
    } catch (err) {
      return this.fail(ERRCODE.SERVER_COMM_ERROR, err.message);
    }
  }
}
